<template>
  <div style="margin-top: 2vh">
    <div style="background-color: white; width: 68vw; margin: auto;">
      <!-- 上方标题 -->
      <div style="border-bottom: 1px solid #e9e9e9; height: 7vh; width: 100%;">
        <span style="font-weight: bold; font-size: 1.1rem; line-height: 7vh; margin-left: 1.5vw;">新闻报道信息</span>
      </div>

      <pdf-preview :pdf-url="form.pdfUrl"></pdf-preview>
    </div>

    <!-- 填写信息 -->
    <div style="background-color: white; width: 68vw; margin: auto; display: flex; flex-direction: column; padding-bottom: 5vh; margin-top: 3vh">
      <div style="flex: 1;">
        <div style="border-bottom: 1px solid #e9e9e9; height: 7vh;">
          <span style="font-weight: bold; font-size: 1.1rem; line-height: 7vh; margin-left: 1.5vw">信息详情</span>
        </div>
        <div>
          <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" style="margin-top: 5vh">
            <el-form-item label="新闻标题" prop="title" required>
              <el-input
                  v-model="form.title"
                  placeholder="请输入新闻标题"
                  style="width: 34vw;"
              ></el-input>
            </el-form-item>

            <el-form-item label="媒体级别" prop="mediaLevel" required>
              <el-select v-model="form.mediaLevel" placeholder="请选择媒体级别" style="width: 34vw;">
                <el-option label="国家级" value="国家级"></el-option>
                <el-option label="省级" value="省级"></el-option>
                <el-option label="市级" value="市级"></el-option>
                <el-option label="区级" value="区级"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="报道时间" prop="reportTime" required>
              <el-date-picker
                  v-model="form.reportTime"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="x"
                  style="width: 34vw;"
              ></el-date-picker>
            </el-form-item>

            <el-form-item label="栏目/版面" prop="columnSection">
              <el-input
                  v-model="form.columnSection"
                  placeholder="如：教育观察/A3"
                  style="width: 34vw;"
              ></el-input>
            </el-form-item>

            <el-form-item label="在线链接" prop="onlineLink">
              <el-input
                  v-model="form.onlineLink"
                  placeholder="如：http://example.com/news"
                  style="width: 34vw;"
              ></el-input>
            </el-form-item>

            <el-form-item label="关联课题" prop="projectId">
              <el-input
                  v-model="form.projectId"
                  placeholder="请选择成果所属课题"
                  style="width: 34vw;"
              ></el-input>
            </el-form-item>

            <el-form-item label="备注信息" prop="remarks">
              <el-input
                  type="textarea"
                  v-model="form.remarks"
                  :rows="4"
                  placeholder="请输入备注信息"
                  style="width: 34vw;"
              ></el-input>
            </el-form-item>
          </el-form>

          <div style="display: flex; justify-content: center; margin-top: 2vh">
            <button @click="submitForm" class="upload-button" style="width: 10vw;">返 回</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import PdfPreview from '@/components/result/PdfPreview.vue';
import {getReport} from "@/api/result/report";
import {getProjectNameById} from "@/api/result/longitudinalProjects";

const route = useRoute()
const router = useRouter()
const formRef = ref(null)

// 表单数据
const form = reactive({
  id: '',
  title: '',
  mediaName: '',
  mediaLevel: '',
  reportTime: '',
  columnSection: '',
  onlineLink: '',
  remarks: '',
  projectId: '',
  syncStatus: '',
  pdfUrl: '',

})

// 表单验证规则
const rules = {
  title: [{ required: true, message: '请输入新闻标题', trigger: 'blur' }],
  mediaName: [{ required: true, message: '请输入媒体名称', trigger: 'blur' }],
  mediaLevel: [{ required: true, message: '请选择媒体级别', trigger: 'change' }],
  reportTime: [{ required: true, message: '请选择报道时间', trigger: 'change' }]
}

// 安全转换日期为时间戳
const parseDateToTimestamp = (dateValue) => {
  if (!dateValue) return null;

  // 处理字符串格式日期（如"2025-08-13"）
  if (typeof dateValue === 'string') {
    const date = new Date(dateValue);
    return date.getTime(); // 转换为毫秒时间戳
  }

  // 处理数字类型时间戳
  if (typeof dateValue === 'number') {
    // 如果是秒级时间戳（10位数字），转换为毫秒级（13位）
    if (dateValue.toString().length === 10) {
      return dateValue * 1000;
    }
    return dateValue;
  }

  return null;
}

// 获取详情数据
onMounted(async () => {
  const id = route.query.id
  if (id) {
    try {
      const res = await getReport(id)
      if (res.code === 200 && res.data) {
        const data = res.data
        // 修复时间显示问题：使用安全转换函数处理日期
        data.reportTime = parseDateToTimestamp(data.reportTime)
        Object.assign(form, data)
      } else {
        ElMessage.error('获取新闻报道信息失败')
      }
    } catch (error) {
      console.error('获取新闻报道信息失败:', error)
      ElMessage.error('获取新闻报道信息失败，请重试')
    }

  }
})

function submitForm() {
  router.back()
}
</script>

<style scoped>
.upload-button {
  color: white;
  height: 3.8vh;
  width: 6.5vw;
  background-color: #00b38a;
  cursor: pointer;
  border: none;
}

.upload-button:hover {
  background-color: #009b77;
}

canvas {
  display: block;
  margin: 0 auto;
  background: white;
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
  transform-origin: top center;
}
</style>
